package response

import (
	"day12_11_homework/config"
	"day12_11_homework/pkg"
	"math/rand/v2"
	"strconv"
	"time"
)

func SendSmsResponse(tel string, source string) string {
	Num := "sendSms_" + source + tel
	Loc := "sendSms_loc_" + source + tel
	get := config.Rdb.Get(config.Ctx, Loc)
	if get.Val() != "" {
		return "请稍后再试"
	}
	i := rand.IntN(9000) + 1000
	resp, err := pkg.SendSms(tel, strconv.Itoa(i))
	if err != nil {
		return "短信发送错误"
	}
	if *resp.Body.Code != "OK" {
		return *resp.Body.Message
	}
	config.Rdb.Set(config.Ctx, Num, i, time.Minute*2)
	config.Rdb.Set(config.Ctx, Loc, "锁", time.Minute*5)
	return ""
}

func GetSendSms(tel string, code string) string {
	Num := "sendSms_register" + tel
	get := config.Rdb.Get(config.Ctx, Num)
	if get.Val() == "" {
		return "暂未获取验证码"
	}
	if get.Val() != code {
		return "验证码错误"
	}
	return ""
}
